//
//  AppTools.m
//  豆瓣终极版
//
//  Created by dllo on 15/12/18.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "AppTools.h"
#import "AFNetworking.h"
@implementation AppTools

+ (void)getDataWithAFNUrl:(NSString *)urlStr block:(AppToolsBlock)block errorBlock:(AppToolsErrorBlock)errorBlock{
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        // 回调
        block(responseObject);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        // 失败时回调
        errorBlock(error);
        
    }];
    
}

+ (NSString *)createFilePathFromDocumentWithFolders:(NSArray<NSString *> *)folders fileName:(NSString *)fileName{
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [NSString stringWithFormat:@"%@", document];
    for (NSString *folder in folders) {
        filePath = [filePath stringByAppendingPathComponent:folder];
        
    }
    [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    filePath = [filePath stringByAppendingPathComponent:fileName];
    return filePath;
}

+ (BOOL)saveDataOnLocal:(NSData *)data localPath:(NSString *)path{
    return [data writeToFile:path atomically:YES];
}

@end
